package com.ogilvy.cms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ogilvy.cms.bean.CmsLog;
import com.ogilvy.cms.bean.CmsResource;
import com.ogilvy.cms.bean.CmsUser;
import com.ogilvy.cms.util.ServiceDispatcher;
import com.ogilvy.common.JSONBean;
import com.ogilvy.common.MetaData;
import com.ogilvy.common.UtilTools;
import com.ogilvy.frame.action.BaseAction;
import com.ogilvy.frame.util.CmsConstants;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("cms.action.CmsResourceAction")
public class CmsResourceAction extends  BaseAction {
	@Autowired
	protected  ServiceDispatcher  serviceDispatcher;
	private String rows;//前台以JSON的形式传递数据
	private int start;//分页开始
	
	private int limit;//每页显示量
	private CmsResource  cmsResource = new CmsResource();
	List <CmsResource>  datalist;
	
	
	public CmsResource getCmsResource() {
		return cmsResource;
	}

	public void setCmsResource(CmsResource cmsResource) {
		this.cmsResource = cmsResource;
	}
	public String resource(){
		return "resource";
	}
	
	
	/**查询资源文件**/
	@SuppressWarnings("unchecked")
	public void query(){
		//解决输入中文的乱码问题
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		
		datalist = serviceDispatcher.getCmsResourceService().query(cmsResource);
		//采用集合分页
		List pageList = UtilTools.queryForPage(start, limit, datalist);
		JSONBean jbean = new JSONBean();
		MetaData md = new MetaData();
		md.setIdProperty("cmrId");//关联将要显示列表对应的bean实体的主键ID
		md.reflectFields("com.ogilvy.cms.bean.CmsResource");//关联将要显示列表对应的bean实体
		jbean.setMetaData(md);
		jbean.setRows(pageList);
		jbean.setSuccess(true);
		jbean.setTotal(datalist.size());
		
		//String jsonString = UtilTools.encodeObject2Json(jbean);
		String jsonString = UtilTools.encodeObject2Json(jbean,"MM/dd/yyyy HH:mm:ss");
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public String getRows() {
		return rows;
	}
	public void setRows(String rows) {
		this.rows = rows;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	/*新增*/
	public void add(){
		System.out.println("cmsresourceAction:addMethod");
		//获取session中user
		CmsUser cmsuser = (CmsUser)ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String cmrAdduser =cmsuser.getCmuName();//获取session中对象的姓名
		//添加资源日志
		CmsLog  cmsLog = UtilTools.buildLog("资源管理", CmsLog.Add, "增加资源", cmrAdduser);
		serviceDispatcher.getCmsLogService().add(cmsLog);
		
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		/** 调用add方法的时候 自动set id**/
		Integer cmrCmpId = js.getInt("cmrCmpId");			//目录ID
		String cmrName = js.getString("cmrName");			//名称 
		Integer cmrTypeId = js.getInt("cmrTypeId");			//类型 
		String cmrFileName = js.getString("cmrFileName");	//文件名 
		Integer cmrOrderVal = js.getInt("cmrOrderVal"); 	//排序值 
		String cmrWidth = js.getString("cmrWidth");			//宽 
		String cmrHeight = js.getString("cmrHeight");		//高 
		String cmrAlt = js.getString("cmrAlt");				//ALT 
		//String cmrAddtime = js.getString("cmrAddtime");	//添加时间 
		//String cmrAdduser = js.getString("cmrAdduser");     //添加人session获取
		//String cmrLasttime= js.getString("cmrLasttime");	//新增时没有修改时间
		//String cmrLastuser= js.getString("cmrLastuser");    //新增时没有修改人
		
		//资源文件主键自增长
		cmsResource.setCmrCmpId(cmrCmpId);
		cmsResource.setCmrName(cmrName);
		cmsResource.setCmrTypeId(cmrTypeId);
		cmsResource.setCmrFileName(cmrFileName);
		cmsResource.setCmrOrderVal(cmrOrderVal);
		cmsResource.setCmrWidth(cmrWidth);
		cmsResource.setCmrHeight(cmrHeight);
		cmsResource.setCmrAlt(cmrAlt);
		cmsResource.setCmrAddtime(new Date());	//添加时，获取当前系统时间
		cmsResource.setCmrAdduser(cmrAdduser);
		//cmsResource.setCmrLasttime(cmrLasttime);				//新增时没有修改时间
		//cmsResource.setCmrLastuser(cmrLastuser);
		CmsResource  cmsresource = serviceDispatcher.getCmsResourceService().add(cmsResource);
		JSONBean bean = new JSONBean();
		bean.setSuccess(true);
		// 需要新建一个具有id的vo返回给前台以便通知前台保存成功，不然前台一直处于编辑状态
		List rows = new ArrayList();
		rows.add(cmsresource);
		bean.setRows(rows);
		//String jsonString = UtilTools.encodeObject2Json(bean);
		String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/** 修改 */
	public void modify(){
		System.out.println("CmsResourceAction ::method:: modify ");
		CmsUser cmsuser = (CmsUser)ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String cmrLastuser = cmsuser.getCmuName();
		CmsLog  cmslog = UtilTools.buildLog("资源管理", CmsLog.MODIFY, "修改资源信息", cmrLastuser);
		serviceDispatcher.getCmsLogService().add(cmslog);
		
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		Integer cmrId= js.getInt("cmrId");
		Integer cmrCmpId = js.getInt("cmrCmpId");
		String cmrName = js.getString("cmrName");
		Integer cmrTypeId = Integer.valueOf(js.getString("cmrTypeId")) ; 
		String cmrFileName = js.getString("cmrFileName");
		Integer cmrOrderVal = Integer.valueOf(js.getString("cmrOrderVal")) ; 
		String cmrWidth = js.getString("cmrWidth");
		String cmrHeight = js.getString("cmrHeight");
		String cmrAlt = js.getString("cmrAlt");
		
//		String cmrAddtime = js.getString("cmrAddtime");		//修改时，获取已存在的添加时间
//		String cmrAdduser = js.getString("cmrAdduser");		//修改时 添加人不变
		//String cmrLasttime= js.getString("cmrLasttime");	//修改时间，获取当前系统时间
//		String cmrLastuser= js.getString("cmrLastuser");
		
		cmsResource = serviceDispatcher.getCmsResourceService().get(cmrId);
		cmsResource.setCmrId(cmrId);
		cmsResource.setCmrCmpId(cmrCmpId);
		cmsResource.setCmrName(cmrName);
		cmsResource.setCmrTypeId(cmrTypeId);
		cmsResource.setCmrFileName(cmrFileName);
		cmsResource.setCmrOrderVal(cmrOrderVal);
		cmsResource.setCmrWidth(cmrWidth);
		cmsResource.setCmrHeight(cmrHeight);
		cmsResource.setCmrAlt(cmrAlt);
//		cmsResource.setCmrAddtime(new Date());				//将添加时间属性封装到cmsResource对象中
//		cmsResource.setCmrAdduser(cmrAdduser);
		cmsResource.setCmrLasttime(new Date());	//修改时间，获取当前系统时间
		cmsResource.setCmrLastuser(cmrLastuser);
		serviceDispatcher.getCmsResourceService().modify(cmsResource);
		JSONBean bean = new JSONBean();
		bean.setSuccess(true);
		//String jsonString = UtilTools.encodeObject2Json(bean);
		String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private Integer cmrId;
	public Integer getCmrId() {
		return cmrId;
	}

	public void setCmrId(Integer cmrId) {
		this.cmrId = cmrId;
	}

	/** 删除 */
	public void del(){
		System.out.println("cmsresourceAction:delXXXMethod");
		CmsUser  cmsSessionUser = (CmsUser)ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String   delResourceUser = cmsSessionUser.getCmuName();
		//资源删除日志添加
		CmsLog cmslog = UtilTools.buildLog("资源管理", CmsLog.DELETE, "删除资源", delResourceUser);
		serviceDispatcher.getCmsLogService().add(cmslog);
		
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		net.sf.json.JSONArray objArr = net.sf.json.JSONArray.fromObject(rows);
        for(int i=0;i<objArr.size();i++)
        {
        	net.sf.json.JSONObject js=objArr.getJSONObject(i);
        	//将 String 转化为 Integer 删除
        	String  cmrIdStr = js.getString("cmrId");
        	System.out.println("获取要删除的id"+cmrIdStr);
        	Integer cmrId = Integer.valueOf(cmrIdStr);
        	serviceDispatcher.getCmsResourceService().del(cmrId);
		}
	     //获取数据
        StringBuffer jsonString =new StringBuffer();
        jsonString.append("{");
        jsonString.append("success:true,");
        jsonString.append("msg:'删除成功'");
        jsonString.append("}");
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}
